package com.rub.android.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.rub.android.R
import com.rub.android.model.ListViewModel


open class ListViewAdapter() : BaseAdapter() {

    private lateinit var dataList: MutableList<ListViewModel>
    private lateinit var context: Context


    constructor(dataList: MutableList<ListViewModel>, context: Context) : this() {
        this.dataList = dataList
        this.context = context
    }

    override fun getCount(): Int = dataList.size

    override fun getItem(position: Int): Any {
        TODO("Not yet implemented")
    }

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int, convertViewx: View?, parent: ViewGroup?): View {

        var viewHolder: ViewHolder
        var convertView: View? = convertViewx
        if (convertView == null) {

            convertView = LayoutInflater.from(context)
                .inflate(R.layout.activity_list_view_item, parent, false)

            viewHolder = ViewHolder()
            viewHolder.textView = convertView?.findViewById(R.id.tv_id)!!
            viewHolder.imageView = convertView.findViewById(R.id.image_view_id);

            convertView.tag = viewHolder
        } else {
            viewHolder = convertView?.tag as ViewHolder
        }

        viewHolder?.textView?.text = dataList[position].title
        viewHolder.imageView.setImageResource(dataList[position].imgId)
        return convertView
    }


    class ViewHolder {
        lateinit var textView: TextView
        lateinit var imageView: ImageView
    }
}

